﻿$.widget("socaloud.twitterQueryWidget", {

    options: {
        template: 'templates/twitter-query-template.html',
        url: 'http://search.twitter.com/search.json',
        query: null
    },

    _create: function () {
        var self = this;

        var content = Extension.getTemplate(self.options.template);
        self.element.append(content);

        self.options.viewModel = {
            page: 1,
            tweets: ko.observableArray([]),
            newTweets: ko.observableArray([]),
            sinceId: null,
            maxId: null,
            parseDate: function (d) {
                var date = new Date(d);
                return date.toTwitterString();
            },
            replyForm: null,
            replyId: null
        };

        ko.applyBindings(self.options.viewModel, self.element[0]);

        Repository.Twitter.getTweets(self.options.query, self.options.viewModel.page, null, function (data) {
            if (!!data && data.results.length > 0) {
                self.options.viewModel.sinceId = data.results[0].id_str;
                self.options.viewModel.maxId = data.results[data.results.length - 1].id_str;
                self.options.viewModel.tweets(data.results || []);
            }
        });


        $('.more-tweets', self.element).off('click').on('click', function () {
            //self.options.viewModel.page++;

            Repository.Twitter.getTweets(self.options.query, self.options.viewModel.page, self.options.viewModel.maxId, function (data) {
                if (!!data && data.results.length > 0) {
                    data.results.shift();
                    var tweets = self.options.viewModel.tweets();
                    var result = tweets.concat(data.results || []);
                    self.options.viewModel.maxId = result[result.length - 1].id_str;
                    self.options.viewModel.tweets(result);
                }
            });
        });

        $('.show-new-tweets', self.element).off('click').on('click', function () {
            var newTweets = self.options.viewModel.newTweets() || [];
            var oldTweets = self.options.viewModel.tweets() || [];

            var tweets = newTweets.concat(oldTweets);
            if (tweets.length > 0) {
                self.options.viewModel.sinceId = tweets[0].id_str;
            }
            self.options.viewModel.newTweets([]);
            self.options.viewModel.tweets(tweets);
        });


        //$(self.element).on('click', '.reply', function () {
        //    var id = $(this).attr('data-id');
        //    if (!!id) {

        //        self.options.viewModel.replyId = id;
        //        if (!!self.options.viewModel.replyForm) {
        //            self.options.viewModel.replyForm.remove();
        //        }

        //        self.options.viewModel.replyForm = $('<div>').html('<textarea id="reply-box"></textarea><input type="button" id="reply-btn" value="Reply" /><input type="button" id="cancel-reply-btn" value="Cancel" />');
        //        $(this).parents('li').append(self.options.viewModel.replyForm);

        //    }
        //});

        $(self.element).on('click', '.retweet', function () {
            var id = $(this).attr('data-id');
            if (!!id && !!confirm('Wanna retweet?')) {
                Repository.Twitter.retweet(id, function () {
                    alert('retweeted!');
                });
            }
        });

        $(self.element).on('click', '.favorite', function () {
            var elem = $(this);
            var id = $(this).attr('data-id');
            Repository.Twitter.favorite(id, function () {
                alert('favorited!');
            });
        });

        $(self.element).on('click', '.delete', function () {
            var elem = $(this);
            var id = elem.attr('data-id');
            if (!!id && !!confirm('Wanna delete?')) {
                Repository.Twitter.delete(id, function () {
                    elem.parents('li').remove();
                });
            }
        });

        $(self.element).on('click', '.expand', function () {
            var elem = $(this);
            var id = elem.attr('data-id');
            if (!!id) {
                var holder = $('<div/>');
                $('.tweet-details', elem.parents('li')).html('').append(holder);
                holder.tweetDetailsWidget({ id: id });
            }
        });

        setInterval(function () { self._checkForTweets(self); }, 15000);
    },

    _checkForTweets: function (instance) {

        Repository.Twitter.checkForTweets(instance.options.query, instance.options.viewModel.sinceId, function (data) {
            if (!!data & data.results.length > 0) {
                instance.options.viewModel.newTweets(data.results);
            }
        });

    },

    _destroy: function () {
        this.element.html('');
    }
});